//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.Interfaces;
using Microsoft.Research.ScientificWorkflow.TridentUtilities;
using Microsoft.Research.ScientificWorkflow.UIDesigner.WpfComposite;
using Microsoft.Practices.Unity;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.WorkflowManagerModule
{
    /// <summary>
    /// The workflow manager module. This represents the left pane controls for the workflow manager and the center pane representing the 
    /// table view.
    /// </summary>
    public class WorkflowMgrModule : IModule
    {
        /// <summary>
        /// The unity container instance.
        /// </summary>
        private IUnityContainer container;

        /// <summary>
        /// The region manager. The main window.
        /// </summary>
        private IRegionManager mainContentControl;
        
        /// <summary>
        /// The center pane of the workflow manager.
        /// </summary>
        private WorkflowManagerContentView contentView;

        /// <summary>
        /// The left pane presentation model.
        /// </summary>
        private WorkflowManagerLeftPanePresenter workflowLeftPanePresenter;

        /// <summary>
        /// Constructor of the workflow manager module.
        /// </summary>
        /// <param name="container">The unity container instance.</param>
        /// <param name="window">The region manager.</param>
        public WorkflowMgrModule(IUnityContainer container, IRegionManager window)
        {
            this.container = container;
            this.mainContentControl = window;
        }

        #region IModule Members

        /// <summary>
        /// Initialize the workflow manager module.
        /// </summary>
        public void Initialize()
        {
            // Create the center pane and insert it in the region manager.
            this.contentView = this.container.Resolve<WorkflowManagerContentView>();
            this.container.RegisterInstance<WorkflowManagerContentView>(this.contentView, new ExternallyControlledLifetimeManager());
            this.mainContentControl.Add(this.contentView, "workflowManagerTablehost");
        
            // Create the left pane control for the module.
            this.workflowLeftPanePresenter = this.container.Resolve<WorkflowManagerLeftPanePresenter>();
          
            // Resolve the left pane controller.
            ILeftPaneNavigator leftPanePresenter = this.container.Resolve<ILeftPaneNavigator>();
            
            // Add the workflow manager control to the left pane.
            leftPanePresenter.Add(ManagementStudioControlsConstants.WORKFLOWMANAGERSTRING, this.workflowLeftPanePresenter);

            // Add the filters for the workflow manager.
            this.InitializeFilters();
        }

        /// <summary>
        /// Initialize the workflow manager left pane filters.
        /// </summary>
        private void InitializeFilters()
        {
            WorkflowManagerFilterModel availableWorkflowModel = WorkflowMgrModule.InitializeAvailableWorkflowFilter();
            AvailableWorkflowsListPresenter availableWorkflowListPresenter = this.container.Resolve<AvailableWorkflowsListPresenter>();
            this.workflowLeftPanePresenter.Add(availableWorkflowModel, availableWorkflowListPresenter);

            WorkflowManagerFilterModel runningWorkflowModel = WorkflowMgrModule.InitializeRunningWorkflowFilter();
            RunningWorkflowsListPresenter runningWorkflowListPresenter = this.container.Resolve<RunningWorkflowsListPresenter>();
            this.workflowLeftPanePresenter.Add(runningWorkflowModel, runningWorkflowListPresenter);

            WorkflowManagerFilterModel completedJobModel = WorkflowMgrModule.InitializeCompletedJobFilter();
            CompletedJobsListViewPresenter completedJobsListPresenter = this.container.Resolve<CompletedJobsListViewPresenter>();
            this.workflowLeftPanePresenter.Add(completedJobModel, completedJobsListPresenter);

            WorkflowManagerFilterModel scheduledWorkflowModel = WorkflowMgrModule.InitializeScheduledJobsFilter();
            ScheduledJobsListPresenter scheduledWorkflowListPresenter = this.container.Resolve<ScheduledJobsListPresenter>();
            this.workflowLeftPanePresenter.Add(scheduledWorkflowModel, scheduledWorkflowListPresenter);

            WorkflowManagerFilterModel deletedWorkflowModel = WorkflowMgrModule.InitializeDeletedWorkflowFilter();
            DeletedWorkflowsListPresenter deletedWorkflowListPresenter = this.container.Resolve<DeletedWorkflowsListPresenter>();
            this.workflowLeftPanePresenter.Add(deletedWorkflowModel, deletedWorkflowListPresenter);
        }

        /// <summary>
        /// Initialize the available workflows filter.
        /// </summary>
        /// <returns>The workflow manager filter model.</returns>
        private static WorkflowManagerFilterModel InitializeAvailableWorkflowFilter()
        {
            WorkflowManagerFilterModel availableWorkflowsFilter = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterAvailableWorkflows"),
                ManagementStudioControlsConstants.AVAILABLEWORKFLOWFILTERNAME,
                ManagementStudioControlsConstants.AVAILABLEWORKFLOWFILTERNAME,
                true);

            WorkflowManagerFilterModel myWorkflows = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterMyWorkflows"),
                ManagementStudioControlsConstants.AVAILABLEWORKFLOWFILTERNAME,
                ManagementStudioControlsConstants.WORKFLOWSCREATEDBYMEFILTERNAME,
                false);
            availableWorkflowsFilter.Children.Add(myWorkflows);

            WorkflowManagerFilterModel recentWorkflows = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterRecentWorkflows"), 
                ManagementStudioControlsConstants.AVAILABLEWORKFLOWFILTERNAME,
                ManagementStudioControlsConstants.RECENTLYCREATEDWORKFLOWS,
                false);
            availableWorkflowsFilter.Children.Add(recentWorkflows);

            return availableWorkflowsFilter;
        }

        /// <summary>
        /// Initialize the Running workflow filter.
        /// </summary>
        /// <returns>The workflow manager filter model.</returns>
        private static WorkflowManagerFilterModel InitializeRunningWorkflowFilter()
        {
            WorkflowManagerFilterModel runningWorkflowsFilter = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterRunningJobs"),
                ManagementStudioControlsConstants.RUNNINGWORKFLOWSFILTERNAME,
                ManagementStudioControlsConstants.RUNNINGWORKFLOWSFILTERNAME,
                false);

            return runningWorkflowsFilter;
        }

        /// <summary>
        /// Initialize the deleted workflow filter.
        /// </summary>
        /// <returns>The deleted workflow filter.</returns>
        private static WorkflowManagerFilterModel InitializeDeletedWorkflowFilter()
        {
            WorkflowManagerFilterModel deletedWorkflowsFilter = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterDeletedWorkflows"),
                ManagementStudioControlsConstants.DELETEDWORKFLOWSFILTERNAME,
                ManagementStudioControlsConstants.DELETEDWORKFLOWSFILTERNAME,
                false);

            WorkflowManagerFilterModel workflowsDeletedByMe = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterWorkflowsDeletedByMe"),
                ManagementStudioControlsConstants.DELETEDWORKFLOWSFILTERNAME,
                ManagementStudioControlsConstants.WORKFLOWSDELETEDBYMEFILTERNAME,
                false);
            deletedWorkflowsFilter.Children.Add(workflowsDeletedByMe);

            WorkflowManagerFilterModel recentDeletedWorkflows = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterRecentlyDeletedWorkflows"),
                ManagementStudioControlsConstants.DELETEDWORKFLOWSFILTERNAME,
                ManagementStudioControlsConstants.RECENTLYDELETEDWORKFLOWS,
                false);
            deletedWorkflowsFilter.Children.Add(recentDeletedWorkflows);

            return deletedWorkflowsFilter;
        }

        /// <summary>
        /// Initialize the completed job filter.
        /// </summary>
        /// <returns>The completed job filter.</returns>
        private static WorkflowManagerFilterModel InitializeCompletedJobFilter()
        {
            WorkflowManagerFilterModel completedWorkflowsFilter = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterCompletedJobs"),
                ManagementStudioControlsConstants.COMPLETEDJOBSFILTERNAME,
                ManagementStudioControlsConstants.COMPLETEDJOBSFILTERNAME,
                false);

            WorkflowManagerFilterModel completedByMe = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterJobsCompletedByMe"),
                ManagementStudioControlsConstants.COMPLETEDJOBSFILTERNAME,
                ManagementStudioControlsConstants.JOBSCOMPLETEDBYMEFILTERNAME,
                false);
            completedWorkflowsFilter.Children.Add(completedByMe);

            WorkflowManagerFilterModel recenltyCompletedWorkflows = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterRecentlyCompletedJobs"),
                ManagementStudioControlsConstants.COMPLETEDJOBSFILTERNAME,
                ManagementStudioControlsConstants.RECENTLYCOMPLETEDJOBS,
                false);
            completedWorkflowsFilter.Children.Add(recenltyCompletedWorkflows);

            return completedWorkflowsFilter;
        }

        /// <summary>
        /// Initialize the scheduled jobs filter.
        /// </summary>
        /// <returns>The scheduled jobs filter.</returns>
        private static WorkflowManagerFilterModel InitializeScheduledJobsFilter()
        {
            WorkflowManagerFilterModel scheduledJobsFilter = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterScheduledJobs"),
                ManagementStudioControlsConstants.SCHEDULEDWORKFLOWSFILTERNAME,
                ManagementStudioControlsConstants.SCHEDULEDWORKFLOWSFILTERNAME,
                false);

            WorkflowManagerFilterModel scheduledByMe = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterScheduledByMe"),
                ManagementStudioControlsConstants.SCHEDULEDWORKFLOWSFILTERNAME,
                ManagementStudioControlsConstants.JOBSSCHEDULEDBYMEFILTERNAME,
                false);
            scheduledJobsFilter.Children.Add(scheduledByMe);

            WorkflowManagerFilterModel recentlyScheduledJobsFilter = new WorkflowManagerFilterModel(ManagementStudioResourceManager.GetString("FilterRecentlyScheduledJobs"),
                ManagementStudioControlsConstants.SCHEDULEDWORKFLOWSFILTERNAME,
                ManagementStudioControlsConstants.RECENTLYSCHEDULEDJOBS,
                false);
            scheduledJobsFilter.Children.Add(recentlyScheduledJobsFilter);

            return scheduledJobsFilter;
        }

        #endregion
    }
}
